﻿@model ProductModel
@inject AdminAreaSettings adminAreaSettings

@{
    var defaultGridPageSize = adminAreaSettings.DefaultGridPageSize;
    var gridPageSizes = adminAreaSettings.GridPageSizes;
}

<div class="panel-group">
    <p>
        @T("Admin.Catalog.Products.StockQuantityHistory.Hint")
    </p>
    @await Component.InvokeAsync("AdminWidget", new { widgetZone = "admin_product_details_stock_quantity_history_top", additionalData = Model.Id })
    @*hide the entire search block if no elements are displayed*@
    <div class="panel panel-default panel-search" @(Model.AvailableWarehouses.SelectionIsNotPossible() ? Html.Raw("style=\"display:none\"") : null)>
        <div class="panel-body">
            <div class="row">
                <div class="col-md-6">
                    <div class="form-group" @(Model.AvailableWarehouses.SelectionIsNotPossible() ? Html.Raw("style=\"display:none\"") : null)>
                        <div class="col-md-4">
                            <nop-label asp-for="StockQuantityHistory.SearchWarehouseId" />
                        </div>
                        <div class="col-md-8">
                            <nop-select asp-for="StockQuantityHistory.SearchWarehouseId" asp-items="Model.AvailableWarehouses" />
                        </div>
                    </div>
                    <div class="form-group">
                        <div class="col-md-8 col-md-offset-4">
                            <button type="button" id="search-stock-quantity-history" class="btn btn-primary btn-search">
                                <i class="fa fa-search"></i>
                                @T("Admin.Common.Search")
                            </button>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <div class="panel panel-default">
        <div class="panel-body">
            <div id="stock-quantity-history-grid"></div>

            <script type="text/javascript">
                $(document).ready(function() {
                    $("#stock-quantity-history-grid").kendoGrid({
                        dataSource: {
                            type: "json",
                            transport: {
                                read: {
                                    url: "@Html.Raw(Url.Action("StockQuantityHistory", "Product"))",
                                    type: "POST",
                                    dataType: "json",
                                    data: additionalData
                                }
                            },
                            schema: {
                                data: "Data",
                                total: "Total",
                                errors: "Errors"
                            },
                            error: function(e) {
                                display_kendoui_grid_error(e);
                                // Cancel the changes
                                this.cancelChanges();
                            },
                            pageSize: @(defaultGridPageSize),
                            serverPaging: true,
                            serverFiltering: true,
                            serverSorting: true
                        },
                        pageable: {
                            refresh: true,
                            pageSizes: [@(gridPageSizes)],
                            @await Html.PartialAsync("_GridPagerMessages")
                        },
                        scrollable: false,
                        columns: [
                            {
                                field: "WarehouseName",
                                title: "@T("Admin.Catalog.Products.StockQuantityHistory.Fields.Warehouse")",
                                width: 200
                            }, {
                                field: "AttributeCombination",
                                title: "@T("Admin.Catalog.Products.StockQuantityHistory.Fields.Combination")",
                                width: 220,
                                encoded: false
                            }, {
                                field: "QuantityAdjustment",
                                title: "@T("Admin.Catalog.Products.StockQuantityHistory.Fields.QuantityAdjustment")",
                                width: 150,
                                template: '# if(QuantityAdjustment > 0) { # +#=QuantityAdjustment# # } else { # #=QuantityAdjustment# # } #'
                            }, {
                                field: "StockQuantity",
                                title: "@T("Admin.Catalog.Products.StockQuantityHistory.Fields.StockQuantity")",
                                width: 130
                            }, {
                                field: "Message",
                                title: "@T("Admin.Catalog.Products.StockQuantityHistory.Fields.Message")"
                            }, {
                                field: "CreatedOn",
                                title: "@T("Admin.Catalog.Products.StockQuantityHistory.Fields.CreatedOn")",
                                type: "date",
                                format: "{0:G}",
                                width: 170
                            }
                        ]
                    });
                });
            </script>

            <script type="text/javascript">
                $(document).ready(function() {
                    $('#search-stock-quantity-history').click(function() {
                        $('#stock-quantity-history-grid').data('kendoGrid').dataSource.page(1);
                        return false;
                    });
                });

                function additionalData() {
                    var data = {
                        productId: @Model.Id,
                        warehouseId: $('#@Html.IdFor(model => model.StockQuantityHistory.SearchWarehouseId)').val()
                    };
                    addAntiForgeryToken(data);
                    return data;
                }
            </script>
        </div>
    </div>
    @await Component.InvokeAsync("AdminWidget", new { widgetZone = "admin_product_details_stock_quantity_history_bottom", additionalData = Model.Id })
</div>